
<template>
  <view class="container">
    <Navbar showNavbar title="钛星服务"/>
    <!-- 搜索区域 -->
    <view class="search-box" @click="handleSearchPage">
      <image style="width: 40rpx;height: 40rpx" src="/static/home/search.png"></image>
      <text class="placeholder">请输入楼盘名称或位置</text>
    </view>

    <!-- 轮播图区域 -->
    <swiper class="banner-box" circular autoplay interval="3000" duration="500">
      <swiper-item v-for="(item, index) in bannerList" :key="index">
        <image class="banner-image" :src="item.image"/>
      </swiper-item>
    </swiper>

    <!-- 热点资讯 -->
    <view class="news-box">
      <image style="width: 118rpx;height: 36rpx;flex-shrink: 0;margin-right: 12rpx" src="/static/home/hot_icon.png"></image>
      <swiper class="news-swiper" vertical circular autoplay :interval="3000" :duration="500">
        <swiper-item>
          <view class="news-text">在黄岛装修露台第一步是做防水，这个钱千万不能省...</view>
        </swiper-item>
        <swiper-item>
          <view class="news-text">青岛二手房市场持续回暖，这些区域最受欢迎...</view>
        </swiper-item>
        <swiper-item>
          <view class="news-text">最新房屋租赁政策解读，租房补贴政策全面落地...</view>
        </swiper-item>
      </swiper>
    </view>

    <!-- 功能区域 -->
    <view class="grid-box">
      <view class="grid-item" @click="toPage(item)" v-for="(item, index) in gridList" :key="index">
        <image class="grid-icon" :src="item.image" mode="aspectFill" />
        <text class="grid-text">{{ item.name }}</text>
      </view>
    </view>

    <view class="tab-box">
      <u-tabs
        :list="tabsList"
        @change="tabsClick"
        :current="current"
        lineColor="#1A8DFF"
        :activeStyle="{
          color: '#333333',
          fontWeight: 'bold',
          fontSize: '36rpx'
        }"
        :inactiveStyle="{
          color: '#333333',
          fontWeight: 'normal',
          fontSize: '36rpx'
        }"
        bgColor="transparent"
      ></u-tabs>
    </view>

    <!-- 房源推荐 -->
    <view class="recommend-box">
      <scroll-view
          scroll-y="true"
          @scrolltolower="onScroll"
          refresher-enabled
          :refresher-triggered="isRefreshing"
          @refresherrefresh="onRefresh"
          style="height: 100%"
      >
        <ItemDetail style="margin: 13px 0" :item="item" v-for="(item, index) in dataList" :key="index" ></ItemDetail>
        <u-loadmore v-if="dataList.length" :status="loading" />
        <div style="height: 40px"></div>
        <u-empty  v-if="dataList.length === 0" mode="list"></u-empty>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import Navbar from "../../components/navbar.vue";
import ItemDetail from "../house/itemDetail.vue";
export default {
  name: "index",
  components: {
    ItemDetail,
    Navbar,
  },
  data() {
    return {
      gridList: [

      ],
      bannerList: [
      ],
      tabsList: [
        { name: '精选楼盘' },
        { name: '热门办公室' }
      ],
      current: 0,
      dataList: [],
      loading: 'loading',
      pageNo: 1,
      total: 0,
      isRefreshing: false,
    }
  },
  methods: {
    handleSearchPage() {
      uni.navigateTo({
        url: '/pages/house/search'
      })
    },
    tabsClick(index) {
      console.log('Tab clicked:', index)
      this.current = index;
      // TODO: Add logic to load different content based on selected tab
      this.getList(true)
    },
    toPage(item) {
      console.log(item)
      uni.navigateTo({
        url: item.url
      })
    },

    async getList(reset = false) {
      if (reset) {
        this.pageNo = 1
        this.dataList = []
      } else {
        if (this.loading === 'nomore') return
      }
      this.loading = 'loading'
      try {
        uni.showLoading({
          title: '加载中...'
        })
        const res = await this.$api.getOfficeBuildingPage({
          pageNum: this.pageNo,
          pageSize: 10,
        })
        let { rows, count } = res
        this.total = count || 0
        if (reset) {
          this.dataList = rows || []
        } else {
          this.dataList = [...this.dataList, ...(rows || [])]
        }

        this.pageNo++
        this.loading = (rows || []).length >= this.total ? 'nomore' : 'loadmore'
        uni.hideLoading()
      } catch (e) {
        console.log(e)
        this.loading = 'loadmore'
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },
    // 下拉刷新
    async onRefresh() {
      if (this.isRefreshing) return
      this.isRefreshing = true

      try {
        await this.getList(true)
        uni.showToast({
          title: '刷新成功',
          icon: 'none'
        })
      } catch (e) {
        uni.showToast({
          title: '刷新失败',
          icon: 'none'
        })
      } finally {
        this.isRefreshing = false
      }
    },

    // 上拉加载更多
    onScroll() {
      if (this.loading === 'loading') return
      this.getList()
    },
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.getList()
  },
  // 分享
  onShareAppMessage() {
    return {
      title: '钛星企服',
      path: '/pages/home/index'
    }
  },
  onLoad() {
    this.$api.getWxMenuList().then(res => {
      console.log(res)
      this.gridList = res
    })
    this.$api.getWxBannerList().then(res => {
      this.bannerList = res
    })

    this.getList(true)
  }
}
</script>

<style>
page {
  height: 100%;
  overflow: auto;
}

.container {
  padding: 0 30rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  background: linear-gradient( 180deg, #E4F6FF 0%, #F7F8FA 100%);
}

.search-box {
  display: flex;
  align-items: center;
  height: 72rpx;
  background-color: #FFFFFF;
  border-radius: 36rpx;
  padding: 0 24rpx;
  margin: 24rpx 0;
}

.placeholder {
  margin-left: 16rpx;
  font-size: 14px;
  color: #999999;
}

.banner-box {
  width: 100%;
  height: 320rpx;
  border-radius: 16rpx;
  overflow: hidden;
  margin-bottom: 24rpx;
}

.banner-image {
  width: 100%;
  height: 100%;
}

.banner-content {
  position: absolute;
  top: 48rpx;
  left: 48rpx;
  color: #FFFFFF;
}

.banner-title {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 16rpx;
  display: block;
}

.banner-subtitle {
  font-size: 14px;
  margin-bottom: 24rpx;
  display: block;
}

.banner-button {
  display: inline-block;
  padding: 12rpx 32rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 24rpx;
  font-size: 14px;
}

.news-box {
  display: flex;
  align-items: center;
  //background-color: #ffffff;

  padding:12rpx 0;
  border-radius: 12rpx;
  margin-bottom: 24rpx;
}

.news-swiper {
  flex: 1;
  height: 40rpx;
}

.news-text {
  margin-left: 16rpx;
  font-size: 14px;
  color: #666666;
  line-height: 40rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.grid-box {
  display: flex;
  flex-wrap: wrap;
  background-color: #F7F8FA;
  border-radius: 12rpx;
  padding: 24rpx 0;
}

.grid-item {
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 24rpx;
}

.grid-icon {
  width: 92rpx;
  height: 92rpx;
}

.grid-text {
  font-size: 26rpx;
  color: #333333;
}

.tab-box {
  display: flex;
  margin-bottom: 24rpx;
}

.tab-item {
  font-size: 16px;
  color: #999999;
  margin-right: 48rpx;
}

.tab-item.active {
  color: #333333;
  font-weight: bold;
  position: relative;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -8rpx;
  left: 0;
  width: 100%;
  height: 4rpx;
  background-color: #1890FF;
  border-radius: 2rpx;
}
.house-item {
  background-color: #FFFFFF;
  border-radius: 12rpx;
  padding: 24rpx;
  margin-bottom: 24rpx;
  display: flex;
}

.house-image {
  width: 240rpx;
  height: 180rpx;
  border-radius: 12rpx;
  margin-right: 32rpx;
  flex-shrink: 0;
}

.house-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.house-title {
  font-size: 32rpx;
  color: #333333;
  font-weight: bold;
  margin-bottom: 12rpx;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.house-detail {
  display: flex;
  align-items: center;
  margin-bottom: 12rpx;
}

.location, .area, .type {
  font-size: 14px;
  color: #666666;
}
.line {
  height: 20rpx;
  width: 2rpx;
  background-color: #DCDCDC;
  margin: 0 24rpx;
}
.house-price {
  display: flex;
  align-items: baseline;
}

.price {
  font-size: 20px;
  color: #FF6B6B;
  font-weight: bold;
}

.unit {
  font-size: 14px;
  color: #FF6B6B;
  margin-left: 4rpx;
}

.unit-price {
  font-size: 14px;
  color: #999999;
}

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #FFFFFF;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1px solid #EEEEEE;
}

.tab-bar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tab-bar-item text {
  font-size: 12px;
  color: #999999;
  margin-top: 4rpx;
}

.tab-bar-item.active text {
  color: #1890FF;
}
</style>
